<?php
/**
 * Created by JetBrains PhpStorm.
 * User: admin
 * Date: 18.10.12
 * Time: 20:27
 * To change this template use File | Settings | File Templates.
 */
class Mylab_Validators_LoginNotMatch extends Zend_Validate_Abstract
{

    /**
     * Returns true if and only if $value meets the validation requirements
     *
     * If $value fails validation, then this method returns false, and
     * getMessages() will return an array of messages that explain why the
     * validation failed.
     *
     * @param  mixed $value
     * @return boolean
     * @throws Zend_Validate_Exception If validation of $value is impossible
     */
    const MATCH = 'match';

    protected $_messageTemplates = array(
        self::MATCH => 'Такой логин уже занят другим пользователем',
    );

    public function isValid($value)
    {
        $dbTable = Zend_Db_Table::getDefaultAdapter();
        $login = $dbTable->fetchRow($dbTable->select()
            ->from('users', array('login'))
            ->where('login = ?', $value)
            ->limit(1));

        if(!$login){
            return true;
        }

        $this->_error(self::MATCH);
        return false;
    }
}
